const path = require('path');
const bodyParser = require('koa-bodyparser');
const logger = require('koa-logger');
const winston = require('winston');
const compress = require('koa-compress');
// for performance improve
global.Promise = require('bluebird');

module.exports = function (app) {
	// use compress
	app.use(compress({threshold: 512}));
	// use winston on production
	if (app.env === 'production') {
		winston.handleExceptions(new winston.transports.File({
			filename: path.resolve(__dirname, '..', 'logs', 'error.log')
		}));
	}
	app.use(logger());
	// body parser
	app.use(bodyParser());
	// error logging
	app.on('error', function (err, ctx) {
		winston.error(err.toString());
		if (ctx) {
			ctx.body = {msg: err.message};
		}
	});
};
